package com.example.sample.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.control.NameValueLayout;

import java.util.ArrayList;

public class BuildInfoActivity extends BaseActivity {

    private static class NameValuePair{
        public String name;
        public String value;
        public NameValuePair(String name, String value){
            this.name = name;
            this.value = value;
        }
    }

    private ListView mListView;
    private ArrayList<NameValuePair> mDataList = new ArrayList<NameValuePair>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_info);
        mListView = (ListView)findViewById(R.id.list);
        mListView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                NameValueLayout nl = null;
                Context ctx = BuildInfoActivity.this;
                if (convertView == null){
                    nl = new NameValueLayout(ctx);
                }else{
                    nl = (NameValueLayout)convertView;
                }
                NameValuePair nvp = mDataList.get(position);
                nl.setContent(nvp.name, nvp.value);
                return nl;
            }
        });

        init();
    }

    private void init(){
        addData("SDK_INT", "" + Build.VERSION.SDK_INT);
        addData("CODENAME", "" + Build.VERSION.CODENAME);
        addData("INCREMENTAL", "" + Build.VERSION.INCREMENTAL);
        addData("RELEASE", "" + Build.VERSION.RELEASE);
        addData("getRadioVersion", "" + Build.getRadioVersion());
        addData("BOARD", "" + Build.BOARD);
        addData("BOOTLOADER", "" + Build.BOOTLOADER);
        addData("商标（BRAND）", "" + Build.BRAND);
        String[] abis;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            abis = Build.SUPPORTED_ABIS;
        }else{
            abis = new String[]{"" + Build.CPU_ABI + Build.CPU_ABI2};
        }
        StringBuilder result = new StringBuilder();
        for (String abi : abis){
            result.append(">");
            result.append(abi);
        }
        addData("CPU_ABI", "" + result.toString());
        addData("DEVICE", "" + Build.DEVICE);
        addData("DISPLAY", "" + Build.DISPLAY);
        addData("指纹（FINGERPRINT）", "" + Build.FINGERPRINT);
        addData("硬件（HARDWARE）", "" + Build.HARDWARE);
        addData("HOST", "" + Build.HOST);
        addData("ID", "" + Build.ID);
        addData("设备厂商（MANUFACTURER）", "" + Build.MANUFACTURER);
        addData("型号（MODEL）", "" + Build.MODEL);
        addData("PRODUCT", "" + Build.PRODUCT);
        addData("SERIAL", "" + Build.SERIAL);
        addData("TAGS", "" + Build.TAGS);
        addData("TYPE", "" + Build.TYPE);
        addData("USER", "" + Build.USER);
        addData("TIME", "" + Build.TIME);

        String iMei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE))
                .getDeviceId();
        addData("IMEI", iMei);
    }

    private void addData(String name, String value){
        mDataList.add(new NameValuePair(name, value));
    }
}
